﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Ledict.Core.Data;
using System.Data.Common;
using Ledict.ConditionExtract.Model;
using System.Data.SqlClient;
using Ledict.Utility.Helper;
using Ledict.ConditionExtract.Common;

namespace Ledict.ConditionExtract.DAL
{
    public class Express_WordDAL
    {
        private string DbName;
        private string DbName2;
        public Express_WordDAL()
        {
            DbName = "JudgeDoc";
            DbName2 = "JudgeProcess";
        }

        #region ExpressWord
        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public bool Exists_Word(SYS_Express_Word Model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from SYS_Express_Word");
            strSql.Append(" where  EW_Content=@EW_Content  and MS_ID =@MS_ID");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@EW_Content", DbType.String, Model.EW_Content);
            oBase.AddInParameter(oCmd, "@MS_ID", DbType.String, Model.MS_ID);
            return Convert.ToInt32(oBase.ExecuteScalar(oCmd).ToString()) > 0 ? true : false;
        }

        /// <summary>
        /// 增加一条数据
        /// </summary>
        public bool Add_Word(SYS_Express_Word Model)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into SYS_Express_Word(");
            strSql.Append("EW_ID,MS_ID,EW_Content,EW_UserID,EW_WordCount,EW_UseCount,EW_CaseId,ParagraphTypeId,EW_WordPurpose)");
            strSql.Append(" values (");
            strSql.Append("@EW_ID,@MS_ID,@EW_Content,@EW_UserID,@EW_WordCount,@EW_UseCount,@EW_CaseId,@ParagraphTypeId,@EW_WordPurpose)");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@EW_ID", DbType.String, Model.EW_ID);
            oBase.AddInParameter(oCmd, "@MS_ID", DbType.String, Model.MS_ID);
            oBase.AddInParameter(oCmd, "@EW_Content", DbType.String, Model.EW_Content);
            oBase.AddInParameter(oCmd, "@EW_UserID", DbType.String, "");
            oBase.AddInParameter(oCmd, "@EW_WordCount", DbType.Int32, Model.EW_WordCount);
            oBase.AddInParameter(oCmd, "@EW_UseCount", DbType.Int32, Model.EW_UseCount);
            oBase.AddInParameter(oCmd, "@EW_CaseId", DbType.String, Model.EW_CaseId);

            oBase.AddInParameter(oCmd, "@ParagraphTypeId", DbType.Int32, Model.ParagraphTypeId);
            oBase.AddInParameter(oCmd, "@EW_WordPurpose", DbType.Int32, Model.EW_WordPurpose);


            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;

        }

        public bool Delete_Word(string EW_ID)
        {
            StringBuilder strSql = new StringBuilder();
            //2017-07-04 ljj 为了将SYS_Express_Word_Process表移除System库做出的修改
            strSql.AppendFormat(@"update Ldt_JudgeProcess.dbo.SYS_Express_Word_Process set EW_Right=0,
	          EW_Wrong=1
	          where EW_ID='{0}'
              delete SYS_Express_Word where EW_ID='{0}'
              ", EW_ID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 根据主键删除SYS_Express_Word
        /// </summary>
        /// <param name="EW_ID"></param>
        /// <returns></returns>
        public bool DeleteEwID(string EW_ID)
        {
            StringBuilder strSql = new StringBuilder();
            //2017-07-04 ljj 为了将SYS_Express_Word_Process表移除System库做出的修改
            strSql.AppendFormat(@" delete SYS_Express_Word where EW_ID='{0}' ", EW_ID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        public bool UpdateEW_IsAvailable(string EW_ID, int EW_IsAvailable)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat(@"update SYS_Express_Word set EW_IsAvailable='{0}' where EW_ID='{1}' ", EW_IsAvailable, EW_ID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        #endregion
        #region ExpressWordProcess



        /// <summary>
        /// 表述语句中是否存在该记录
        /// </summary>
        public bool IsExistsData(SYS_Express_Word WordModel)
        {
            string str = string.Format(@" select COUNT(1) from dbo.SYS_Express_Word_Process where EW_CaseId='{0}' and MS_ID in(select CC_ID FROM Ldt_JudgeDocSystem.dbo.SYS_CaseCause  where ','+AllParentId+',' like '%,'+'{1}'+',%' and CC_ID<>'{1}') ", WordModel.EW_CaseId, WordModel.MS_ID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(str.ToString());
            if (Convert.ToInt32(oBase.ExecuteScalar(oCmd)) > 0)
            {
                return true;
            }
            return false;
        }



        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public string Exists_WordProcess(SYS_Express_Word Model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 EW_ID from SYS_Express_Word_Process");
            strSql.Append(" where EW_WordCount=@EW_WordCount and  EW_Content=@EW_Content  and MS_ID =@MS_ID and EW_IsSingleExpress=@EW_IsSingleExpress");
            //2017-07-04 ljj 为了将SYS_Express_Word_Process表移除System库做出的修改
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@EW_Content", DbType.String, Model.EW_Content);
            oBase.AddInParameter(oCmd, "@MS_ID", DbType.String, Model.MS_ID);
            oBase.AddInParameter(oCmd, "@EW_WordCount", DbType.String, Model.EW_WordCount);
            oBase.AddInParameter(oCmd, "@EW_IsSingleExpress", DbType.String, string.IsNullOrEmpty(Model.EW_IsSingleExpress.ToString()) ? 0 : Model.EW_IsSingleExpress);
            return LdFormat.ObjectToString(oBase.ExecuteScalar(oCmd));
        }

        /// <summary>
        /// 验证是否存在该纪录（争议焦点专用）
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public string Exists_WordProcess1(SYS_Express_Word Model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select top 1 EW_ID from SYS_Express_Word_Process");
            strSql.Append(" where EW_WordCount=@EW_WordCount and  EW_Content=@EW_Content  and EW_CaseId =@EW_CaseId");
            //2017-07-04 ljj 为了将SYS_Express_Word_Process表移除System库做出的修改
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@EW_Content", DbType.String, Model.EW_Content);
            oBase.AddInParameter(oCmd, "@EW_CaseId", DbType.String, Model.EW_CaseId);
            oBase.AddInParameter(oCmd, "@EW_WordCount", DbType.String, Model.EW_WordCount);
            return LdFormat.ObjectToString(oBase.ExecuteScalar(oCmd));
        }

        /// <summary>
        /// 添加句
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public bool AddWord_Process(SYS_Express_Word Model)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                strSql.AppendFormat(@"insert into SYS_Express_Word_Process (EW_ID,MS_ID,EW_Content,EW_UserID,EW_WordCount,EW_UseCount,EW_CaseId,ParagraphTypeId,EW_Right,EW_Wrong,EW_IsSingleExpress)
                          values ('{0}','{1}','{2}','{3}',{4},{5},'{6}',{7},{8},{9},{10})",
                              Model.EW_ID, Model.MS_ID, Model.EW_Content.Replace("'", "''"), Model.EW_UserID, Model.EW_WordCount,
                              Model.EW_UseCount, Model.EW_CaseId, Model.ParagraphTypeId==null?0: Model.ParagraphTypeId, Model.EW_Right, Model.EW_Wrong, Model.EW_IsSingleExpress == null ? 0 : Model.EW_IsSingleExpress);
                //2017-07-04 ljj 为了将SYS_Express_Word_Process表移除System库做出的修改
                Database oBase = DatabaseFactory.CreateDatabase(DbName2);
                DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

                //oBase.AddInParameter(oCmd, "@EW_ID", DbType.String, Model.EW_ID);
                //oBase.AddInParameter(oCmd, "@MS_ID", DbType.String, Model.MS_ID);
                //oBase.AddInParameter(oCmd, "@EW_Content", DbType.String, Model.EW_Content);
                //oBase.AddInParameter(oCmd, "@EW_UserID", DbType.String, Model.EW_UserID);
                //oBase.AddInParameter(oCmd, "@EW_WordCount", DbType.Int32, Model.EW_WordCount);
                //oBase.AddInParameter(oCmd, "@EW_UseCount", DbType.Int32, Model.EW_UseCount);
                //oBase.AddInParameter(oCmd, "@EW_CaseId", DbType.String, Model.EW_CaseId);
                //oBase.AddInParameter(oCmd, "@ParagraphTypeId", DbType.Int32, Model.ParagraphTypeId);
                //oBase.AddInParameter(oCmd, "@EW_Wrong", DbType.Int32, Model.EW_Wrong);
                //oBase.AddInParameter(oCmd, "@EW_Right", DbType.Int32, Model.EW_Right);

                if (oBase.ExecuteNonQuery(oCmd) > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 添加句
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public bool AddWordProcessNew(SYS_Express_Word Model)
        {
            try
            {
                serverhelper sqlserverhelper = new serverhelper("");
                string primaryKey = sqlserverhelper.CreateKeyID();
                StringBuilder strSql = new StringBuilder();
                strSql.AppendFormat(@"insert into SYS_Express_Word_Process  (EW_ID,MS_ID,EW_Content,EW_UserID,EW_CaseId,ParagraphTypeId,Ruleword,Word)
                          values (@EW_ID,@MS_ID,@EW_Content,@EW_UserID,@EW_CaseId,@ParagraphTypeId,@Ruleword,@Word)");
                //primaryKey, Model.MS_ID, Model.EW_Content,"znsjb", Model.EW_CaseId,Model.ParagraphTypeId, Model.Ruleword, Model.Word);
                //2017-07-04 ljj 为了将SYS_Express_Word_Process表移除System库做出的修改
                Database oBase = DatabaseFactory.CreateDatabase(DbName2);
                DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
                oBase.AddInParameter(oCmd, "@EW_ID", DbType.String, primaryKey);
                oBase.AddInParameter(oCmd, "@MS_ID", DbType.String, Model.MS_ID);
                oBase.AddInParameter(oCmd, "@EW_Content", DbType.String, Model.EW_Content);
                oBase.AddInParameter(oCmd, "@EW_UserID", DbType.String, "znsjb");
                oBase.AddInParameter(oCmd, "@EW_CaseId", DbType.String, Model.EW_CaseId);
                oBase.AddInParameter(oCmd, "@ParagraphTypeId", DbType.Int32, Model.ParagraphTypeId);
                oBase.AddInParameter(oCmd, "@Ruleword", DbType.String, Model.Ruleword);
                oBase.AddInParameter(oCmd, "@Word", DbType.String, Model.Word);

                if (oBase.ExecuteNonQuery(oCmd) > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 添加句
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public bool AddWordAnalysis(string strSql)
        {
            try
            {
                Database oBase = DatabaseFactory.CreateDatabase(DbName2);
                DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
                if (oBase.ExecuteNonQuery(oCmd) > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {

                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 修改word的钩和差
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public bool UpdateWordProcessNew(string ewId, int type, string remark = "")
        {
            string sql = string.Empty;
            //0打对勾修改
            if (type == 0)
            {
                sql = string.Format(@" update SYS_Express_Word_ProcessCase set EW_Right=1,EW_Wrong=0 where EW_ID='{0}' ", ewId);
            }
            else//1打×修改
            if (type == 1)
            {
                sql = string.Format(@" update SYS_Express_Word_ProcessCase set EW_Wrong=1,EW_Right=0 where EW_ID='{0}' ", ewId);
            }
            else if (type == 2)//2保存备注
            {
                sql = string.Format(@" update SYS_Express_Word_ProcessCase set Remark='{0}' where EW_ID='{1}' ", remark, ewId);
            }
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        /// 获取民事智能数据部用数据
        /// </summary>
        /// <param name="msID">节点ccid</param>
        /// <param name="pagesize">每页显示条数</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns></returns>
        public DataTable GetExtraceContentNew(string msID, int pagesize, int pageIndex, string condition)
        {
            string strSql = string.Format(@" 		select * from(	 select top {1} * from(
select   row_number() over( order by EW_ID )as showindex, a.EW_ID  as EW_ID , a.MS_ID ,a.EW_Content as EW_Content ,a.EW_CaseId ,a.EW_Right,a.EW_Wrong,
case a.ParagraphTypeId when 0 then '本院认为' else '审理查明' end ParagraphType,a.Ruleword,a.Remark,a.EW_UseCount,c.AM_Name as DataSign,ISNULL(b.num,0) XSNum
from SYS_Express_Word_ProcessCase a
left join (select AM_ID,AM_Name from  Ldt_JudgeDocSystem.dbo.SYS_Argument where AT_ID=(select AT_ID from Ldt_JudgeDocSystem.dbo.SYS_ArgumentType where AT_Code='casemark'))c  on c.AM_ID=a.DataSign
left join (select count(1) as num,PrimaryData_ID from SYS_Express_Word_ProcessCase group by PrimaryData_ID)b on b.PrimaryData_ID=a.EW_ID
where  MS_ID='{0}' {3} and a.Flag=1 )b where showindex>{2}  ) c order by EW_UseCount desc
 ", msID, pagesize, (pageIndex - 1) * pagesize, condition);
            // and b.EW_IsSingleExpress={1} , isSingleExpress
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql);
            oCmd.CommandTimeout = 3000;
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }



        /// <summary>
        /// 获取相似度数据
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public DataTable GetXSData(string ewID)
        {
            string strSql = string.Format(@" select EW_Content,PrimaryData_ID from SYS_Express_Word_ProcessCase where PrimaryData_ID='{0}'  ", ewID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql);
            oCmd.CommandTimeout = 300;
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }


        /// <summary>
        /// 获取需要重新处理的数据
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public int GetProcessCaseCount(string ccID, string condition)
        {
            string strSql = string.Format(@" select count(1) from Ldt_JudgeProcess.dbo.SYS_Express_Word_ProcessCase where MS_ID ='{0}' {1} and Flag=1 ", ccID, condition);
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql);
            oCmd.CommandTimeout = 6000;
            return Convert.ToInt32(oBase.ExecuteScalar(oCmd).ToString());
        }

        /// <summary>
        /// 获取民事智能数据部用数据
        /// </summary>
        /// <param name="msID">节点ccid</param>
        /// <param name="pagesize">每页显示条数</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns></returns>
        public DataTable GetGPCData(string msID, int zongNum)
        {
            string strSql = string.Format(@"select Word as content,CAST(CONVERT(decimal(18,2), COUNT(1)*1.0/{1})*100 as varchar(50))+'%' as CP from  SYS_Express_Word_Analysis where MS_ID='{0}' group by Word 
having CONVERT(decimal(18,2), COUNT(1)*1.0/{1})>0.5
 ", msID, zongNum);
            // and b.EW_IsSingleExpress={1} , isSingleExpress
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql);
            oCmd.CommandTimeout = 300;
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }


        /// <summary>
        /// 重新处理时删除数据
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public bool DeleteProcessNew(string ccID, int paragraphTypeId)
        {
            string sql = string.Format(@" delete from Ldt_JudgeProcess.dbo.SYS_Express_Word_ProcessCase where MS_ID in(select CC_ID  from Ldt_JudgeDocSystem.dbo.SYS_CaseCause where CHARINDEX(',{0},',','+AllParentId+',')>0) and ParagraphTypeId={1} and  EW_Right<>1    ", ccID, paragraphTypeId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                sql = string.Format(@" delete from Ldt_JudgeProcess.dbo.SYS_Express_Word_Analysis where MS_ID in(select CC_ID  from Ldt_JudgeDocSystem.dbo.SYS_CaseCause where CHARINDEX(',{0},',','+AllParentId+',')>0)   ", ccID);
                oCmd = oBase.GetSqlStringCommand(sql.ToString());
                oBase.ExecuteNonQuery(oCmd);
                return true;
            }
            return false;
        }

        /// <summary>
        /// 重新处理时删除数据
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public bool DeleteProcessNew1(string ccID)
        {
            string sql = string.Format(@" delete from Ldt_JudgeProcess.dbo.SYS_Express_Word_ProcessCase where MS_ID in(select CC_ID  from Ldt_JudgeDocSystem.dbo.SYS_CaseCause where CHARINDEX(',{0},',','+AllParentId+',')>0)  and  EW_Right<>1    ", ccID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                sql = string.Format(@" delete from Ldt_JudgeProcess.dbo.SYS_Express_Word_Analysis where MS_ID in(select CC_ID  from Ldt_JudgeDocSystem.dbo.SYS_CaseCause where CHARINDEX(',{0},',','+AllParentId+',')>0)   ", ccID);
                oCmd = oBase.GetSqlStringCommand(sql.ToString());
                oBase.ExecuteNonQuery(oCmd);
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取需要重新处理的数据
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public DataTable GetProcessNew(string ccID, int paragraphTypeId)
        {
            string strSql = string.Format(@" select distinct EW_Content,ParagraphTypeId,EW_CaseId,IsContainsFT,EW_UseCount from Ldt_JudgeProcess.dbo.SYS_Express_Word_ProcessCase where MS_ID in(select CC_ID  from Ldt_JudgeDocSystem.dbo.SYS_CaseCause where CHARINDEX(',{0},',','+AllParentId+',')>0) and ParagraphTypeId={1}  ", ccID, paragraphTypeId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql);
            oCmd.CommandTimeout = 300;
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 获取需要重新处理的数据
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public DataTable GetProcessNew1(string ccID)
        {
            string strSql = string.Format(@" select distinct EW_Content,ParagraphTypeId,EW_CaseId,IsContainsFT,EW_UseCount  from Ldt_JudgeProcess.dbo.SYS_Express_Word_ProcessCase where MS_ID in(select CC_ID  from Ldt_JudgeDocSystem.dbo.SYS_CaseCause where CHARINDEX(',{0},',','+AllParentId+',')>0)   ", ccID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql);
            oCmd.CommandTimeout = 300;
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 获取需要重新处理的数据
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public int GetProcessCount(string ccID)
        {
            string strSql = string.Format(@" select count(1) from Ldt_JudgeProcess.dbo.SYS_Express_Word_ProcessCase where MS_ID ='{0}'  ", ccID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql);
            oCmd.CommandTimeout = 6000;
            return Convert.ToInt32(oBase.ExecuteScalar(oCmd).ToString());
        }

        /// <summary>
        /// 导出当前节点的数据
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public DataSet DaoChuData(string ccID, int type)
        {
            string strSql = string.Empty;
            if (type == 1)
            {
                strSql = string.Format(@"  select ROW_NUMBER()over(order by EW_ID) as 序号,EW_Content as 表述,case ParagraphTypeId when 0 then '本院认为' else '审理查明' end as 逻辑段,EW_CaseId as caseID,ISNULL(Remark,'') as 备注
from Ldt_JudgeProcess.dbo.SYS_Express_Word_Process where  MS_ID='{0}' and EW_UserID = 'znsjb' ", ccID);
            }
            else if (type == 2)
            {
                strSql = string.Format(@"  select ROW_NUMBER()over(order by EW_ID) as 序号,EW_Content as 表述,case ParagraphTypeId when 0 then '本院认为' else '审理查明' end as 逻辑段,EW_CaseId as caseID,ISNULL(Remark,'') as 备注
from Ldt_JudgeProcess.dbo.SYS_Express_Word_Process where  MS_ID in(select CC_ID  from Ldt_JudgeDocSystem.dbo.SYS_CaseCause where CHARINDEX(',{0},',','+AllParentId+',')>0)  and EW_UserID = 'znsjb' ", ccID);
            }
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql);
            DataSet OtB = oBase.ExecuteDataSet(oCmd);
            return OtB;
        }



        /// <summary>
        /// 添加句
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public bool UpdateWordProcess(SYS_Express_Word Model)
        {
            string strSql = string.Format(@"update  SYS_Express_Word_Process set MS_ID='{0}' where EW_ID='{1}'", Model.MS_ID, Model.EW_ID);
            //2017-07-04 ljj 为了将SYS_Express_Word_Process表移除System库做出的修改
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            //oBase.AddInParameter(oCmd, "@EW_ID", DbType.String, Model.EW_ID);
            //oBase.AddInParameter(oCmd, "@MS_ID", DbType.String, Model.MS_ID);
            //oBase.AddInParameter(oCmd, "@EW_Content", DbType.String, Model.EW_Content);
            //oBase.AddInParameter(oCmd, "@EW_UserID", DbType.String, Model.EW_UserID);
            //oBase.AddInParameter(oCmd, "@EW_WordCount", DbType.Int32, Model.EW_WordCount);
            //oBase.AddInParameter(oCmd, "@EW_UseCount", DbType.Int32, Model.EW_UseCount);
            //oBase.AddInParameter(oCmd, "@EW_CaseId", DbType.String, Model.EW_CaseId);
            //oBase.AddInParameter(oCmd, "@ParagraphTypeId", DbType.Int32, Model.ParagraphTypeId);
            //oBase.AddInParameter(oCmd, "@EW_Wrong", DbType.Int32, Model.EW_Wrong);
            //oBase.AddInParameter(oCmd, "@EW_Right", DbType.Int32, Model.EW_Right);

            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        /// 修改IsSingleExpress字段为0（表示该条数据被多个节点所匹配）
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public bool UpdateWord_ProcessIsSingleExpress(int EW_IsSingleExpress, string MS_ID, string EW_ID)
        {
            try
            {
                StringBuilder strSql = new StringBuilder();
                string sql = string.Format(@" select MS_ID from Ldt_JudgeDocSystem.dbo.SYS_Express_MatchSituation where SituationID = '{0}'", MS_ID);
                Database oBase = DatabaseFactory.CreateDatabase(DbName2);
                DbCommand oCmd = oBase.GetSqlStringCommand(sql.ToString());
                string msIDD = LdFormat.ObjectToString(oBase.ExecuteScalar(oCmd));
                if (!string.IsNullOrEmpty(msIDD))
                {
                    MS_ID = msIDD;
                }
                strSql.Append(@"
		    update dbo.SYS_Express_Word_Process set EW_IsSingleExpress=@IsSingleExpress ,MS_ID=@MS_ID
		    where EW_ID=@EW_ID");
                //2017-07-04 ljj 为了将SYS_Express_Word_Process表移除System库做出的修改
                Database oBase1 = DatabaseFactory.CreateDatabase(DbName2);
                DbCommand oCmd1 = oBase1.GetSqlStringCommand(strSql.ToString());
                oBase1.AddInParameter(oCmd1, "@EW_ID", DbType.String, EW_ID);
                oBase1.AddInParameter(oCmd1, "@IsSingleExpress", DbType.Int32, EW_IsSingleExpress);
                oBase1.AddInParameter(oCmd1, "@MS_ID", DbType.String, MS_ID);

                if (oBase1.ExecuteNonQuery(oCmd1) > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 已存在修改次数，要素词
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public bool UpdateWord_Process(SYS_Express_Word Model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"
		    update dbo.SYS_Express_Word_Process set EW_UseCount=EW_UseCount+1 
		    where EW_ID=@EW_ID");
            //2017-07-04 ljj 为了将SYS_Express_Word_Process表移除System库做出的修改
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@EW_ID", DbType.String, Model.EW_ID);

            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 更新CC_IsShowExpress
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public bool UpdateCC_IsShowExpress(string ID, int state)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"
             declare @Count int
             select @Count =Count(*) from  dbo.SYS_CaseCause  where  CC_ID=@ID
             if(@Count>0)
             begin
             update   SYS_CaseCause
             set CC_IsShowExpress=@state where CC_ID=@ID
             end
             else
             update dbo.SYS_Express_MatchSituation  
             set CC_IsShowExpress=@state where  MS_ID =@ID
            ");
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@ID", DbType.String, ID);
            oBase.AddInParameter(oCmd, "@state", DbType.Int32, state);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public SYS_Express_Word GetModel_Process(string EW_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select  top 1 EW_ID,MS_ID,EW_Content,EW_UserID,EW_WordCount,EW_UseCount,EW_Right,EW_Wrong,EW_CaseId,ParagraphTypeId from SYS_Express_Word_Process ");
            strSql.Append(" where EW_ID=@EW_ID  ");
            //2017-07-04 ljj 为了将SYS_Express_Word_Process表移除System库做出的修改
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@EW_ID", DbType.String, EW_ID);

            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return DataRowToModel(OtB.Rows[0]);
            }
            else
            {
                return new SYS_Express_Word();
            }
        }
        public SYS_Express_Word DataRowToModel(DataRow row)
        {
            SYS_Express_Word model = new SYS_Express_Word();
            if (row != null)
            {
                if (row["EW_ID"] != null)
                {
                    model.EW_ID = row["EW_ID"].ToString();
                }

                if (row["MS_ID"] != null)
                {
                    model.MS_ID = row["MS_ID"].ToString();
                }
                if (row["EW_Content"] != null)
                {
                    model.EW_Content = row["EW_Content"].ToString();
                }
                if (row["EW_UserID"] != null)
                {
                    model.EW_UserID = row["EW_UserID"].ToString();
                }
                if (row["EW_WordCount"] != null && row["EW_WordCount"].ToString() != "")
                {
                    model.EW_WordCount = int.Parse(row["EW_WordCount"].ToString());
                }
                if (row["EW_UseCount"] != null && row["EW_UseCount"].ToString() != "")
                {
                    model.EW_UseCount = int.Parse(row["EW_UseCount"].ToString());
                }
                if (row["EW_Right"] != null && row["EW_Right"].ToString() != "")
                {
                    model.EW_Right = int.Parse(row["EW_Right"].ToString());
                }
                if (row["EW_Wrong"] != null && row["EW_Wrong"].ToString() != "")
                {
                    model.EW_Wrong = int.Parse(row["EW_Wrong"].ToString());
                }
                if (row["EW_CaseId"] != null)
                {
                    model.EW_CaseId = row["EW_CaseId"].ToString();
                }

                if (row["ParagraphTypeId"] != null && row["ParagraphTypeId"].ToString() != "")
                {
                    model.ParagraphTypeId = int.Parse(row["ParagraphTypeId"].ToString());
                }
            }
            return model;
        }



        /// <summary>
        /// 修改word的钩和差
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public bool UpdateChooseState_Process(SYS_Express_Word Model)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"update SYS_Express_Word_Process set EW_Right=@EW_Right,
	          EW_Wrong=@EW_Wrong,EW_UserID='0',EW_WordPurpose=@EW_WordPurpose
	          where EW_ID=@EW_ID
              ");
            //2017-07-04 ljj 为了将SYS_Express_Word_Process表移除System库做出的修改
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@EW_ID", DbType.String, Model.EW_ID);
            oBase.AddInParameter(oCmd, "@EW_Right", DbType.String, Model.EW_Right);
            oBase.AddInParameter(oCmd, "@EW_Wrong", DbType.String, Model.EW_Wrong);
            oBase.AddInParameter(oCmd, "@EW_WordPurpose", DbType.Int32, Model.EW_WordPurpose);


            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 暂不重跑的要素
        /// </summary>
        /// <param name="EW_ID"></param>
        /// <param name="EW_AddElement"></param>
        /// <returns></returns>
        public bool UpdateAddElement_Process(string EW_ID, string EW_AddElement)
        {
            //2017-07-04 ljj 为了将SYS_Express_Word_Process表移除System库做出的修改
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(@"update SYS_Express_Word_Process  set
	        EW_AddElement=   isnull(EW_AddElement,'')+@EW_AddElement
	        where EW_ID=@EW_ID ");

            oBase.AddInParameter(oCmd, "@EW_ID", DbType.String, EW_ID);

            oBase.AddInParameter(oCmd, "@EW_AddElement", DbType.String, EW_AddElement);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 两个表（改值）
        /// </summary>
        /// <param name="EW_ID"></param>
        /// <param name="EW_Content"></param>
        /// <returns></returns>
        public bool UpdateContent(string EW_ID, string EW_Content)
        {
            SqlConnectionStringBuilder sqlbuilder = new SqlConnectionStringBuilder(ConfigHelper.ConnectionStrings("JudgeProcess").ConnectionString);
            StringBuilder strSql = new StringBuilder();
            //2017-07-04 ljj 为了将SYS_Express_Word_Process表移除System库做出的修改
            strSql.AppendFormat(@"update {0}.dbo.SYS_Express_Word_Process   set
	         EW_Content='{1}' ,EW_WordCount=LEN('{1}')
	         where EW_ID='{2}' 
             update SYS_Express_Word   set
	         EW_Content='{1}' ,EW_WordCount=LEN('{1}')
	         where EW_ID='{2}' 
	         ", sqlbuilder.InitialCatalog, EW_Content, EW_ID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            //oBase.AddInParameter(oCmd, "@EW_ID", DbType.String, EW_ID);
            //oBase.AddInParameter(oCmd, "@EW_Content", DbType.String, EW_Content);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }
        /// <summary>
        /// 清空保存的要素
        /// </summary>
        /// <returns></returns>
        public bool UpdateClearElement_Process()
        {
            StringBuilder strSql = new StringBuilder();
            //2017-07-04 ljj 为了将SYS_Express_Word_Process表移除System库做出的修改
            strSql.Append(@"update SYS_Express_Word_Process   set
	           EW_AddElement='' 
	         ");
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="EW_ID"></param>
        /// <returns></returns>
        public bool Delete_WordProcess(string EW_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"delete  SYS_Express_Word_Process where EW_ID=@EW_ID
              ");
            //2017-07-04 ljj 为了将SYS_Express_Word_Process表移除System库做出的修改
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            oBase.AddInParameter(oCmd, "@EW_ID", DbType.String, EW_ID);

            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }
        #endregion

        /// <summary>
        /// 要素是否存在
        /// </summary>
        /// <param name="headword"></param>
        /// <param name="wordBankKeys"></param>
        /// <returns></returns>
        public bool IswordBankKeys(string headword, string wordBankKeys)
        {
            //             StringBuilder strSql = new StringBuilder();
            //             strSql.Append(@"select Count(*)  from dbo.SYS_Express_Word_Process  as a 
            //            join dbo.SYS_Express_MatchSituation as c
            //            on a.MS_ID=c.MS_ID
            //            where 
            //            isnull(EW_AddElement,'')= @wordBankKeys and  SituationID=@headword
            //              ");
            //             Database oBase = DatabaseFactory.CreateDatabase(DbName);
            //             DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            //             oBase.AddInParameter(oCmd, "@headword", DbType.String, headword);
            //             oBase.AddInParameter(oCmd, "@wordBankKeys", DbType.String, wordBankKeys);

            //             return Convert.ToInt32(oBase.ExecuteScalar(oCmd).ToString()) > 0 ? true : false;
            return false;
        }
        /// <summary>
        /// 所有节点的表述中的字典词组（伪节点）
        /// </summary>
        /// <param name="CCID"></param>
        /// <returns></returns>
        public DataTable GetAllwordBankKeys(string MS_ID)
        {
            SqlConnectionStringBuilder sqlbuilder = new SqlConnectionStringBuilder(ConfigHelper.ConnectionStrings("JudgeProcess").ConnectionString);
            StringBuilder strSql = new StringBuilder();
            //2017-07-04 ljj 为了将SYS_Express_Word_Process表移除System库做出的修改
            strSql.AppendFormat(@"
             select  a.WBK_ID,count(a.WBK_ID)as ElementCount , (select  WBK_Word from dbo.SYS_WordBank_KeyWords 
             where WBK_ID=a.WBK_ID) as Element ,'+添加' AddE   from dbo.SYS_Express_WordBankRel a 
             join {0}.dbo.SYS_Express_Word_Process b 
             on (a.EW_ID=b.EW_ID and b.MS_ID='{1}')
             group by WBK_ID
             order by count(a.WBK_ID) desc
              ", sqlbuilder.InitialCatalog, MS_ID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            //oBase.AddInParameter(oCmd, "@MS_ID", DbType.String, MS_ID);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }



        public bool AddSYS_Express_SituationWordRel(string ES_IDs, string EW_ID)
        {
            //2017-07-04 ljj 为了将SYS_Express_Word_Process表移除System库做出的修改
            //Database oBase = DatabaseFactory.CreateDatabase(DbName);
            //DbCommand oCmd = oBase.GetStoredProcCommand("sp_CondiExt_AddExpress_SituationWordRel");

            //oBase.AddInParameter(oCmd, "@ES_IDs", DbType.String, ES_IDs);
            //oBase.AddInParameter(oCmd, "@EW_ID", DbType.String, EW_ID);


            //if (oBase.ExecuteNonQuery(oCmd) > 0)
            //{
            //    return true;
            //}
            return false;
        }

        public DataTable GetExractContent(string id, string sort, string asc, string SelectText, string SelectWBK_ID, int effective)
        {
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetStoredProcCommand("sp_CondiExt_GetExractContent");
            oBase.AddInParameter(oCmd, "@id", DbType.String, id);
            oBase.AddInParameter(oCmd, "@sort", DbType.String, sort);
            oBase.AddInParameter(oCmd, "@asc", DbType.String, asc);
            oBase.AddInParameter(oCmd, "@SelectText", DbType.String, SelectText);
            oBase.AddInParameter(oCmd, "@SelectWBK_ID", DbType.String, SelectWBK_ID);
            oBase.AddInParameter(oCmd, "@effective", DbType.Int32, effective);

            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0 && id != "")
            {
                return OtB;
                //DataView dv = new DataView(OtB);
                //DataTable dt = dv.ToTable(true,  "EW_ID", "MS_ID", "EW_Content", "EW_UseCount", "EW_CaseId", "EW_Wrong");
                //return dt;
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 根据MSID获取表数据
        /// </summary>
        /// <param name="msId"></param>
        /// <returns></returns>
        public DataTable GetExtractContentByMsID(string msId)
        {
            SqlConnectionStringBuilder sqlbuilder = new SqlConnectionStringBuilder(ConfigHelper.ConnectionStrings("JudgeProcess").ConnectionString);
            string strSql = string.Format(" select  row_number() over( order by EW_ID )as showindex  ,  EW_ID  as EW_ID , MS_ID ,EW_Content as EW_Content ,EW_UseCount  as EW_UseCount ,EW_CaseId ,isnull(EW_Right,0) as EW_Right,isnull(EW_Wrong,0) as EW_Wrong, case EW_WordPurpose when 1 then '争议焦点' when 2 then '评议结论' else '争议焦点' end  EW_WordPurpose  from {1}.dbo.SYS_Express_Word_Process where MS_ID in( {0}) order by EW_ID ", msId, sqlbuilder.InitialCatalog);
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }


        /// <summary>
        /// 根据MSID获取表数据
        /// </summary>
        /// <param name="msId"></param>
        /// <returns></returns>
        public DataTable GetExtractContentByMsID(string ccId, string AffirmIdeaResult)
        {
            SqlConnectionStringBuilder sqlbuilder = new SqlConnectionStringBuilder(ConfigHelper.ConnectionStrings("JudgeProcess").ConnectionString);
            string strSql = string.Format(" select  row_number() over( order by EW_ID )as showindex  ,  EW_ID  as EW_ID , MS_ID ,EW_Content as EW_Content ,EW_UseCount  as EW_UseCount ,EW_CaseId ,isnull(EW_Right,0) as EW_Right,isnull(EW_Wrong,0) as EW_Wrong, case EW_WordPurpose when 1 then '争议焦点' when 2 then '评议结论' when 3 then '起诉书' when 4 then '事实要素' when 5 then '理由要素' when 6 then '基本要素' else '' end  EW_WordPurpose  from {2}.dbo.SYS_Express_Word_Process where MS_ID in( select MS_ID from 	Ldt_JudgeDocSystem.dbo.SYS_Express_MatchSituation   where SituationID = '{0}'  and AffirmIdeaResult ={1}) order by EW_ID ", ccId, AffirmIdeaResult, sqlbuilder.InitialCatalog);
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql);
            oCmd.CommandTimeout = 180;
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 获取表述加工数据
        /// </summary>
        /// <param name="ms_Id"></param>
        /// <param name="isSingleExpress"></param>
        /// <returns></returns>
        public DataTable GetExtraceContent(string ms_Id, int isSingleExpress)
        {
            string strSql = string.Format(@"   select  row_number() over( order by a.EW_ID  )as showindex  ,   a.EW_ID  as EW_ID   ,    a.MS_ID ,a.EW_Content as EW_Content ,a.EW_UseCount  
                                                 as EW_UseCount,a.EW_CaseId, ISNULL(0,EW_IsAvailable) as EW_IsAvailable,a.UpdateTime, case a.EW_WordPurpose when 1 then '争议焦点' when 2 then '评议结论' when 3 then '起诉书' when 4 then '事实要素' when 5 then '理由要素' when 6 then '基本要素' else '' end EW_WordPurpose  from   
                                                 dbo.SYS_Express_Word a   join Ldt_JudgeProcess.dbo.SYS_Express_Word_Process b  on a.EW_ID=b.EW_ID
                                                 where a.MS_ID in ('{0}')  order by a.EW_ID  ", ms_Id);
            // and b.EW_IsSingleExpress={1} , isSingleExpress
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }


        public DataTable GetExpressContent(string id, string msid, int isSingle, string sort, string IsASC, string SelectText)
        {
            string strOrder = "order by EW_Right desc,EW_UseCount desc";
            if (sort != "")
            {
                strOrder = string.Format("order by {0} {1}", sort, IsASC);
            }
            string strSql = string.Format(@"select row_number() over( {0} )as showindex  ,  EW_ID  as EW_ID , MS_ID ,EW_Content as EW_Content ,EW_UseCount as EW_UseCount, EW_CaseId , isnull(EW_Right, 0) as EW_Right, isnull(EW_Wrong, 0) as EW_Wrong,EW_UserID from Ldt_JudgeProcess.dbo.SYS_Express_Word_Process
              where (MS_ID in({1}) or MS_ID ='{2}') and EW_IsSingleExpress= {3} {4} {0}", strOrder, string.IsNullOrEmpty(msid) ? "''" : msid, id, isSingle, SelectText);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql);
            oCmd.CommandTimeout = 3000;
            oBase.AddInParameter(oCmd, "@ID", DbType.String, id);
            oBase.AddInParameter(oCmd, "@MS_ID", DbType.String, msid);
            oBase.AddInParameter(oCmd, "@EW_IsSingleExpress", DbType.Int32, isSingle);

            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0 && id != "")
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        public DataTable GetExpressContent(string id)
        {
            string strSql = string.Format(@" select * from Ldt_JudgeProcess.dbo.SYS_Express_Word_Process  where MS_ID in( select MS_ID from dbo.SYS_Express_MatchSituation
		                                    where AllStituationIDs='{0}'  and SituationID = '{0}' and (AllParentID='-1' or AllParentID=MS_ID) )", id);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0 && id != "")
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        public DataTable GetExpressContent1(string ccid)
        {
            string strSql = string.Format(@" select 
		                                     MS_ID,isnull(EW_Right, 0) as EW_Right, isnull(EW_Wrong, 0) as EW_Wrong
		                                       from Ldt_JudgeProcess.dbo.SYS_Express_Word_Process
              where (MS_ID in (select MS_ID from dbo.SYS_Express_MatchSituation
		                                    where AllStituationIDs='{0}'  and SituationID = '{0}'
		                                     and IsMatch =1
		                                    and AffirmIdeaResult=1) or MS_ID = '{0}') 
		                                    and EW_IsSingleExpress=0 ", ccid);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0 && ccid != "")
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 查询和词库关联的数据
        /// </summary>
        /// <param name="WBK_ID"></param>
        /// <param name="MS_ID"></param>
        /// <returns></returns>
        public DataTable GetExractContent_Wordbook(string WBK_ID, string MS_ID)
        {
            SqlConnectionStringBuilder sqlbuilder = new SqlConnectionStringBuilder(ConfigHelper.ConnectionStrings("JudgeProcess").ConnectionString);
            StringBuilder strSql = new StringBuilder();
            //2017-07-04 ljj 为了将SYS_Express_Word_Process表移除System库做出的修改
            strSql.AppendFormat(@"
            select    row_number() over(order by  b.EW_ID) as showindex  ,b.EW_ID  as EW_ID   , 
            b.MS_ID ,b.EW_Content as EW_Content ,
            b.EW_UseCount as EW_UseCount,
            b.EW_CaseId from dbo.SYS_Express_WordBankRel a 
            join {0}.dbo.SYS_Express_Word_Process b
            on a.EW_ID=b.EW_ID
            where  WBK_ID ='{1}'
            and MS_ID='{2}'
              ", sqlbuilder.InitialCatalog, WBK_ID, MS_ID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            //oBase.AddInParameter(oCmd, "@WBK_ID", DbType.String, WBK_ID);
            //oBase.AddInParameter(oCmd, "@MS_ID", DbType.String, MS_ID);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        public DataTable GetExractContent_AddWord(string id, string ElementWord)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(@"  select  row_number() over( order by  EW_ID )as showindex  ,   EW_ID  as EW_ID   ,EW_Content as EW_Content ,EW_UseCount
	         as EW_UseCount,EW_CaseId ,MS_ID  from dbo.SYS_Express_Word_Process  
             where MS_ID=@id and EW_Content like '%'+@ElementWord+'%'         
              ");
            //2017-07-04 ljj 为了将SYS_Express_Word_Process表移除System库做出的修改
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            oBase.AddInParameter(oCmd, "@id", DbType.String, id);
            oBase.AddInParameter(oCmd, "@ElementWord", DbType.String, ElementWord);
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }


        }

        /// <summary>
        /// 删除表述节点及其表述内容
        /// </summary>
        /// <param name="CC_ID">节点id</param>
        /// <returns></returns>
        public bool DeleteSYS_Express_Word_Process(string CC_ID)
        {
            try
            {
                SqlConnectionStringBuilder sqlbuilder = new SqlConnectionStringBuilder(ConfigHelper.ConnectionStrings("JudgeProcess").ConnectionString);
                string strSql = string.Format(@"delete from {0}.dbo.SYS_Express_Word_Process 
                                                where MS_ID in(select MS_ID from dbo.SYS_Express_MatchSituation 
                                                              where SituationID in (select CC_ID from SYS_CaseCause  where CHARINDEX(',{1},',','+AllParentId+',')>0)) 
                                                and EW_Right != 1;          delete  from {0}.dbo.SYS_Express_Word_Process
              where ( MS_ID in(select CC_ID from SYS_CaseCause  where CHARINDEX(',{1},',','+AllParentId+',')>0)) and EW_IsSingleExpress=1 and EW_Right != 1  ",
                    sqlbuilder.InitialCatalog, CC_ID);
                Database oBase = DatabaseFactory.CreateDatabase(DbName);
                DbCommand oCmd = oBase.GetSqlStringCommand(strSql);
                oCmd.CommandTimeout = 300;
                if (oBase.ExecuteNonQuery(oCmd) >= 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 插入删除日志
        /// </summary>
        /// <param name="tableName"></param>
        /// <param name="tableKey"></param>
        /// <param name="tableKeyValue"></param>
        /// <returns></returns>
        public bool InsertDeleteLog(string tableName, string tableKey, string tableKeyValue)
        {
            string strSql = string.Format(" insert into DeleteLog (TableName,TableKey,TableKeyValue,DeleteDateTime)values('{0}','{1}','{2}','{3}') ", tableName, tableKey, tableKeyValue, DateTime.Now);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql);
            if (oBase.ExecuteNonQuery(oCmd) >= 0)
            {
                return true;
            }
            return false;

        }

        /// <summary>
        /// 删除表述节点及其表述内容
        /// </summary>
        /// <param name="CC_ID">节点id</param>
        /// <returns></returns>
        public bool DeleteSYS_Express_Word_ProcessMinshi(string CC_ID)
        {
            try
            {
                string strSql = string.Format(@"  delete from Ldt_JudgeProcess.dbo.SYS_Express_Word_Process
 where MS_ID in (select MS_ID from Ldt_JudgeDocSystem.dbo.SYS_Express_MatchSituation where SituationID in(select CC_ID from Ldt_JudgeDocSystem.dbo.SYS_CaseCause where CHARINDEX(',{0},',','+AllParentId+',')>0))and EW_Right != 1 ;  delete from SYS_Express_MatchSituation where SituationID in (select CC_ID from SYS_CaseCause  where CHARINDEX(',{0},',','+AllParentId+',')>0) and MS_ID not in(  select distinct MS_ID from Ldt_JudgeProcess.dbo.SYS_Express_Word_Process
          where MS_ID in (select MS_ID from Ldt_JudgeDocSystem.dbo.SYS_Express_MatchSituation where SituationID in(select CC_ID from Ldt_JudgeDocSystem.dbo.SYS_CaseCause where CHARINDEX(',{0},',','+AllParentId+',')>0))and EW_Right = 1 
        )", CC_ID);
                Database oBase = DatabaseFactory.CreateDatabase(DbName);
                DbCommand oCmd = oBase.GetSqlStringCommand(strSql);
                if (oBase.ExecuteNonQuery(oCmd) >= 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        /// <summary>
        /// 是否存在该记录
        /// </summary>
        public string Exists_WordProcessCase(SYS_Express_Word_ProcessCase Model)
        {
            string strSql = string.Format(" select EW_ID from SYS_Express_Word_ProcessCase where EW_Content='{0}' and MS_ID ='{1}'  ", Model.EW_Content, Model.MS_ID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            return LdFormat.ObjectToString(oBase.ExecuteScalar(oCmd));
        }

        /// <summary>
        /// 添加SYS_Express_Word_ProcessCase表
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public bool AddSYS_ExpressWordProcessCase(SYS_Express_Word_ProcessCase Model)
        {
            try
            {

                serverhelper sqlserverhelper = new serverhelper("");
                string primaryKey = sqlserverhelper.CreateKeyID();
                string strSql = string.Format(@" insert into SYS_Express_Word_ProcessCase  (EW_ID,MS_ID,EW_Content,EW_UserID,EW_CaseId,ParagraphTypeId,Ruleword,IsContainsFT,EW_UseCount)
                          values('{0}', '{1}', '{2}', '{3}', '{4}',{5}, '{6}', {7},{8}) ", primaryKey, Model.MS_ID, Model.EW_Content, Model.EW_UserID, Model.EW_CaseId, Model.ParagraphTypeId, Model.Ruleword, Model.IsContainsFT, Model.EW_UseCount);
                Database oBase = DatabaseFactory.CreateDatabase(DbName2);
                DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
                if (oBase.ExecuteNonQuery(oCmd) > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex)
            {
                Log.ErrorLog("添加SYS_Express_Word_ProcessCase表出错：" + ex.Message.ToString());
                return false;
            }
        }

        /// <summary>
        /// 已存在修改次数
        /// </summary>
        /// <param name="Model"></param>
        /// <returns></returns>
        public bool UpdateWord_ProcessCase(string ewId)
        {
            string strSql = string.Format(@" update SYS_Express_Word_ProcessCase set  EW_UseCount=EW_UseCount+1 where EW_ID='{0}' ", ewId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 修改数据标记
        /// </summary>
        /// <param name="eWIDs"></param>
        /// <param name="dataSign"></param>
        /// <returns></returns>
        public bool UpdateProcessCaseDataSign(string eWIDs, string dataSign)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat(@" update [dbo].[SYS_Express_Word_ProcessCase] set DataSign='{0}' where EW_ID in({1}) ", dataSign, eWIDs);
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取案由下的msids
        /// </summary>
        /// <param name="causeId"></param>
        /// <returns></returns>
        public DataTable GetMSIDS(string causeId)
        {
            string strSql = string.Format("  select distinct MS_ID from SYS_Express_Word_ProcessCase where  MS_ID in (select cc_id FROM Ldt_JudgeDocSystem.dbo.SYS_CaseCause where  CHARINDEX(',{0},',','+AllParentId+',')>0) ", causeId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }


        /// <summary>
        /// 获取ewId和内容
        /// </summary>
        /// <param name="causeId"></param>
        /// <returns></returns>
        public DataTable GetEwIdAndContent(string causeId)
        {
            string strSql = string.Format(@"  select EW_ID,MS_ID,EW_Content from SYS_Express_Word_ProcessCase where MS_ID in (select cc_id FROM Ldt_JudgeDocSystem.dbo.SYS_CaseCause where  CHARINDEX(',{0},',','+AllParentId+',')>0) ", causeId);
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        /// <summary>
        /// 修改数据标记
        /// </summary>
        /// <param name="eWIDs"></param>
        /// <param name="dataSign"></param>
        /// <returns></returns>
        public bool UpdateProcessFlag(string eWID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat(@" update [dbo].[SYS_Express_Word_ProcessCase] set Flag='1' where EW_ID ='{0}' ", eWID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }

        /// <summary>
        /// 修改数据标记
        /// </summary>
        /// <param name="eWIDs"></param>
        /// <param name="dataSign"></param>
        /// <returns></returns>
        public bool UpdateProcessFlag(string parenteWID, string eWID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.AppendFormat(@" update [dbo].[SYS_Express_Word_ProcessCase] set Flag='2',PrimaryData_ID='{0}' where EW_ID ='{1}' ", parenteWID, eWID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        /// 获取智慧检务用筛选数据
        /// </summary>
        /// <param name="msID"></param>
        /// <returns></returns>
        public DataTable GetMSID(string msID)
        {
            string strSql = string.Format(@"  select* from SYS_Express_Word where MS_ID='{0}' and EW_WordPurpose=3 ", msID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        public DataTable GetExpressWordData(string name)
        {
            string strSql = string.Format("   SELECT MS_ID  FROM[Ldt_JudgeDocSystem].[dbo].[SYS_Express_Word] where EW_Content like '%{0}%' and (EW_WordPurpose<>3 or EW_WordPurpose is null ) and (EW_Content is not null or EW_Content<>'' )  group by MS_ID   ", name);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }

        }

        public DataTable GetExpressWordData1(string msid, string name)
        {
            string strSql = string.Format("  SELECT top 1 *  FROM [Ldt_JudgeDocSystem].[dbo].[SYS_Express_Word] where MS_ID='{0}' and EW_Content like '%{1}%'  order by EW_UseCount desc ", msid, name);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }

        }

        /// <summary>
        /// 表述库加工用
        /// </summary>
        /// <param name="msid"></param>
        /// <returns></returns>
        public DataTable GetExpressWordData2(string msid)
        {
            if (!string.IsNullOrEmpty(msid))
            {
                string strSql = string.Format(" select row_number() over( order by a.EW_ID  )as showindex  ,   a.EW_ID  as EW_ID   ,    a.MS_ID ,a.EW_Content as EW_Content ,a.EW_UseCount  as EW_UseCount, a.EW_CaseId, ISNULL(EW_IsAvailable,0) as EW_IsAvailable, a.UpdateTime, case a.EW_WordPurpose when 1 then '争议焦点' when 2 then '评议结论' when 3 then '起诉书' when 4 then '事实要素' when 5 then '理由要素' when 6 then '基本要素' else '' end EW_WordPurpose  from  SYS_Express_Word a where MS_ID in( {0}) order by EW_ID ", msid);
                Database oBase = DatabaseFactory.CreateDatabase(DbName);
                DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
                DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
                if (OtB.Rows.Count > 0)
                {
                    return OtB;
                }
                else
                {
                    return new DataTable();
                }
            }
            else
            {
                return new DataTable();
            }
        }


        /// <summary>
        /// 表述库加工用
        /// </summary>
        /// <param name="msid"></param>
        /// <returns></returns>
        public DataTable GetExpressWordData3()
        {
            string strSql = " select EW_ID,EW_Content from Ldt_JudgeProcess.dbo.SYS_Express_Word_Process where EW_Right=1 ";
            Database oBase = DatabaseFactory.CreateDatabase(DbName2);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }

        public string Exists_WordProcess(string ewID)
        {
            string strSql = string.Format(" select  EW_Content from SYS_Express_Word where EW_ID='{0}'  ", ewID);
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            return LdFormat.ObjectToString(oBase.ExecuteScalar(oCmd));
        }

        public bool UpdateWordContent(string EW_ID, string EW_Content)
        {
            string strSql = string.Format(" update SYS_Express_Word set  EW_Content='{0}'  where EW_ID='{1}'  ", EW_Content, EW_ID);

            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());

            //oBase.AddInParameter(oCmd, "@EW_ID", DbType.String, EW_ID);
            //oBase.AddInParameter(oCmd, "@EW_Content", DbType.String, EW_Content);
            if (oBase.ExecuteNonQuery(oCmd) > 0)
            {
                return true;
            }
            return false;
        }


        /// <summary>
        /// 表述库加工用
        /// </summary>
        /// <param name="msid"></param>
        /// <returns></returns>
        public DataTable GetExpressWordData4()
        {
            string strSql = @" select CC_ID,AllParentId from SYS_CaseCause where CC_ID in(select SituationID from SYS_Express_MatchSituation where MS_ID in(
select MS_ID from Ldt_JudgeProcess.dbo.SYS_Express_Word_Process where EW_Right = 1)) and CC_IsShowExpress = 0 and AllParentId like '%1477%' ";
            Database oBase = DatabaseFactory.CreateDatabase(DbName);
            DbCommand oCmd = oBase.GetSqlStringCommand(strSql.ToString());
            DataTable OtB = oBase.ExecuteDataSet(oCmd).Tables[0];
            if (OtB.Rows.Count > 0)
            {
                return OtB;
            }
            else
            {
                return new DataTable();
            }
        }
    }
}
